<style lang="scss" scoped>
.shop-card {
  box-sizing: border-box;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;

  .img {
    width: 100%;
    //transition: all 0.5s;
    object-fit: cover;
  }

  .title {
    height: 22px;
    padding-top: 5px;
    padding-left: 5px;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 13px;
    font-family: -apple-system-font, Helvetica Neue, Arial, sans-serif;
  }

  .price {
    color: #fA2C19;
    margin-top: 10px;
    padding-left: 5px;
    text-align: left;
  }

  .holder {
    height: 14px;
  }
}

.shop-line {
  display: flex;
  align-items: center;
  background: #fff;

  .img {
    width: 100px;
    height: 100px;
    overflow: hidden;

    img {
      width: 100%;
    }
  }

  .block {
    display: flex;
    flex-direction: column;
    flex: 1;
    justify-content: center;
    padding-left: 10px;

    .title {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 34px;
      padding-left: 5px;
      text-align: left;
      font-size: 13px;
      font-family: -apple-system-font, Helvetica Neue, Arial, sans-serif;
    }

    .price {
      color: #fA2C19;
      margin-top: 10px;
      padding-left: 5px;
      text-align: left;
    }
  }
}
</style>

<template>
  <div :class="types" ref="card" @click="showDetail(shopData)">
    <img class="img" ref="img" :src="shopData.swiper[0]"/>
    <div class="block">
      <div class="title van-multi-ellipsis--l1">{{ shopData.proname }}</div>
      <div class="price" style="position: relative"><span style="font-size: 14px">￥</span><span style="font-size: 17px">{{
          getPrice(shopData)
        }}</span> <span
          style="margin-left: 4px;color: #bbbbbb;font-size: 12px;text-decoration: line-through">￥{{
          shopData.originprice
        }}</span>
        <van-tag color="#ffe1e1" text-color="#ad0000"
                 style="position: absolute; right: 5px; top: 50%;transform: translateY(-50%);margin-top: 2px">
          {{ shopData.discount }}折
        </van-tag>
      </div>
      <div class="holder"></div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { requestAddFootPrint } from '../../api/footprint'
import { Image as VanImage } from 'vant'

Vue.use(VanImage)

export default {
  name: 'ShopCard',
  props: {
    shopData: {
      type: Object,
      default () {
        return {}
      }
    },
    types: {
      type: String,
      default: 'shop-card'
    },
  },
  data () {
    return {
      height: 300
    }
  },
  mounted () {
    try {
      this.$refs.img.height = this.$refs.img.width
      window.addEventListener('resize', () => {
        try {
          if (this.$refs.img&&getComputedStyle(this.$refs.img)['width'] === '100%') {
            this.$refs.img.height = innerWidth * 0.38 - 3
          } else {
            if (this.$refs.img.width)
              this.$refs.img.height = this.$refs.img.width
          }
        } catch (e) {
          return
        }
      })
    } catch (e) {
      return
    }
  },
  methods: {
    getPrice (value) {
      return Math.floor(value.originprice * value.discount / 10)
    },
    showDetail (value) {
      console.log('wgar')
      requestAddFootPrint(this.shopData.proid).then(res => {
        console.log(res)
      })
      this.$router.push('/shopDetail/' + value.proid)
    }
  },
  watch: {
    types (val) {
      this.types = val
    },
  }
}
</script>
